package com.kanesoft.android.gdx.actors.fight;

import com.badlogic.gdx.math.Vector2;
import com.kanesoft.android.gdx.actors.ActorPhysicsParameters;
import com.kanesoft.android.gdx.widgets.sprites.SpriteInfo;

public class FighterJumping extends FightActorStateMachine {

    private FightActorObject fightActor;
    private FightActorStateMachine previousState;
    private boolean alreadyJump;

    public FighterJumping(SpriteInfo stateSpriteInfo, FightActorObject actor) {
        super(stateSpriteInfo, actor);
        fightActor = actor;
    }

    @Override
    public boolean OnEnter() {

        if (fightActor.getActorState() != null) {
            if (((fightActor.getActorState().equals(fightActor.waitingState)) ||  
             (fightActor.getActorState().equals(fightActor.walkingBackwardState)) || 
             (fightActor.getActorState().equals(fightActor.walkingForwardState)) ) == false) return false;
 
        }

        previousState = fightActor.getActorState();

        actorSprite.setAnimationInLoop(false);
        actorSprite.setSpriteInfo(stateSpriteInfo);
        
        alreadyJump = false;

        return true;
    }

    @Override
    public boolean OnUpdate() {
        
        
        if ((alreadyJump)&&(actor.getWorldControl().isObjectOnTheGround(actor) == false)) {
            actor.setActorState(fightActor.fallingState);
            return false;
        }
        
        if (alreadyJump) return true;
        
        alreadyJump = true;

        fightActor.getSensorFixture().setFriction(0.2f);

        Vector2 objPosition = fightActor.getActorBody().getPosition();
        ActorPhysicsParameters objectPhysicsParams = fightActor.getPhysicsParameters();

        fightActor.getActorBody().setTransform(objPosition.x, objPosition.y + 0.01f, 0);

        float forceInX = 0;

        if (previousState.equals(fightActor.walkingBackwardState)) {
            forceInX = -objectPhysicsParams.bodyJumpForce;

            if (fightActor.getHorizontalFlip() == true) {
                forceInX = objectPhysicsParams.bodyJumpForce;
            }
        }

        if (previousState.equals(fightActor.walkingForwardState)) {
            forceInX = objectPhysicsParams.bodyJumpForce;

            if (fightActor.getHorizontalFlip() == true) {
                forceInX = -objectPhysicsParams.bodyJumpForce;
            }
        }

        fightActor.getActorBody().applyLinearImpulse(forceInX, objectPhysicsParams.bodyJumpForce, objPosition.x,
                objPosition.y);

     

        return true;
    }

    @Override
    public String getStateName() {
        return "Jumping";
    }

}
